<!-- 运动记录编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改运动记录':'添加运动记录'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
      
      <el-form-item label="头像:">
        <uploadImage :limit="1" v-model="form.avatar"></uploadImage>
      </el-form-item>
                
      <el-form-item label="类型:" prop="type">
        <el-select
          clearable
          class="ele-block"
          v-model="form.type"
          placeholder="请选择类型">
                  <el-option label="体育测试" :value="1"/>
                  <el-option label="体育锻炼" :value="2"/>
                  <el-option label="体育赛事" :value="3"/>
                </el-select>
      </el-form-item>
                              
      <el-form-item label="学生ID:" prop="memberId">
        <el-input-number
          :min="0"
          v-model="form.memberId"
          placeholder="请输入学生ID"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item label="学校ID(所属组织):" prop="schoolId">
        <el-input-number
          :min="0"
          v-model="form.schoolId"
          placeholder="请输入学校ID(所属组织)"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item label="年级ID:" prop="gradeId">
        <el-input-number
          :min="0"
          v-model="form.gradeId"
          placeholder="请输入年级ID"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item label="班级ID:" prop="classId">
        <el-input-number
          :min="0"
          v-model="form.classId"
          placeholder="请输入班级ID"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item label="院系ID:" prop="departmentsId">
        <el-input-number
          :min="0"
          v-model="form.departmentsId"
          placeholder="请输入院系ID"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item label="专业ID:" prop="professionalId">
        <el-input-number
          :min="0"
          v-model="form.professionalId"
          placeholder="请输入专业ID"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item label="项目ID:" prop="projectId">
        <el-input-number
          :min="0"
          v-model="form.projectId"
          placeholder="请输入项目ID"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item label="任务ID:" prop="taskId">
        <el-input-number
          :min="0"
          v-model="form.taskId"
          placeholder="请输入任务ID"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item
        label="姓名:"
        prop="testerName">
        <el-input
          :maxlength="20"
          v-model="form.testerName"
          placeholder="请输入姓名"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="学籍号:"
        prop="testerNumber">
        <el-input
          :maxlength="20"
          v-model="form.testerNumber"
          placeholder="请输入学籍号"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="身份证号:"
        prop="idCard">
        <el-input
          :maxlength="20"
          v-model="form.idCard"
          placeholder="请输入身份证号"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="性别:"
        prop="gender">
        <el-input
          :maxlength="20"
          v-model="form.gender"
          placeholder="请输入性别"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label=":"
        prop="inviteCode">
        <el-input
          :maxlength="20"
          v-model="form.inviteCode"
          placeholder="请输入"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="学校:"
        prop="school">
        <el-input
          :maxlength="20"
          v-model="form.school"
          placeholder="请输入学校"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="年级:"
        prop="grade">
        <el-input
          :maxlength="20"
          v-model="form.grade"
          placeholder="请输入年级"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label=":"
        prop="clazz">
        <el-input
          :maxlength="20"
          v-model="form.clazz"
          placeholder="请输入"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label=":"
        prop="deviceUnicode">
        <el-input
          :maxlength="20"
          v-model="form.deviceUnicode"
          placeholder="请输入"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label=":"
        prop="itemCode">
        <el-input
          :maxlength="20"
          v-model="form.itemCode"
          placeholder="请输入"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="成绩:"
        prop="score">
        <el-input
          :maxlength="20"
          v-model="form.score"
          placeholder="请输入成绩"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="单位:"
        prop="unit">
        <el-input
          :maxlength="20"
          v-model="form.unit"
          placeholder="请输入单位"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="时间:"
        prop="duration">
        <el-input
          :maxlength="20"
          v-model="form.duration"
          placeholder="请输入时间"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label=":"
        prop="startTime">
        <el-input
          :maxlength="20"
          v-model="form.startTime"
          placeholder="请输入"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label=":"
        prop="endTime">
        <el-input
          :maxlength="20"
          v-model="form.endTime"
          placeholder="请输入"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label=":"
        prop="createdDate">
        <el-input
          :maxlength="20"
          v-model="form.createdDate"
          placeholder="请输入"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label=":"
        prop="reportUrl">
        <el-input
          :maxlength="20"
          v-model="form.reportUrl"
          placeholder="请输入"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label=":"
        prop="videoUrlList">
        <el-input
          :maxlength="20"
          v-model="form.videoUrlList"
          placeholder="请输入"
          clearable/>
      </el-form-item>
                </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'SportRecordEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
              avatar: [
          {required: true, message: '请输入头像', trigger: 'blur'}
        ],
                  
        type: [
          {required: true, message: '请选择类型', trigger: 'blur'}
        ],
                          memberId: [
          {required: true, message: '请输入学生ID', trigger: 'blur'}
        ],
                  schoolId: [
          {required: true, message: '请输入学校ID(所属组织)', trigger: 'blur'}
        ],
                  gradeId: [
          {required: true, message: '请输入年级ID', trigger: 'blur'}
        ],
                  classId: [
          {required: true, message: '请输入班级ID', trigger: 'blur'}
        ],
                  departmentsId: [
          {required: true, message: '请输入院系ID', trigger: 'blur'}
        ],
                  professionalId: [
          {required: true, message: '请输入专业ID', trigger: 'blur'}
        ],
                  projectId: [
          {required: true, message: '请输入项目ID', trigger: 'blur'}
        ],
                  taskId: [
          {required: true, message: '请输入任务ID', trigger: 'blur'}
        ],
                  testerName: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ],
                  testerNumber: [
          {required: true, message: '请输入学籍号', trigger: 'blur'}
        ],
                  idCard: [
          {required: true, message: '请输入身份证号', trigger: 'blur'}
        ],
                  gender: [
          {required: true, message: '请输入性别', trigger: 'blur'}
        ],
                  inviteCode: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
                  school: [
          {required: true, message: '请输入学校', trigger: 'blur'}
        ],
                  grade: [
          {required: true, message: '请输入年级', trigger: 'blur'}
        ],
                  clazz: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
                  deviceUnicode: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
                  itemCode: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
                  score: [
          {required: true, message: '请输入成绩', trigger: 'blur'}
        ],
                  unit: [
          {required: true, message: '请输入单位', trigger: 'blur'}
        ],
                  duration: [
          {required: true, message: '请输入时间', trigger: 'blur'}
        ],
                  startTime: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
                  endTime: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
                  createdDate: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
                  reportUrl: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
                  videoUrlList: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/sportrecord/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
